# Maintainer: @naveen521kk on Github <naveen521kk@gmail.com>

_realname=github-cli
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.33.0
pkgrel=1
pkgdesc='The GitHub CLI (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/cli/cli"
license=('spdx:MIT')
depends=("winpty")
makedepends=("${MINGW_PACKAGE_PREFIX}-go" "${MINGW_PACKAGE_PREFIX}-cc" "git")
optdepends=("git: To interact with repositories")
options=('!strip')
source=("$_realname-$pkgver.tar.gz::$url/archive/v$pkgver.tar.gz"
        "gh")
sha256sums=('27e22a8e637501ee8e9d45c702dbc5c5c559c2a4fe59cd3d807e075cdbefddd2'
            '9ee5f2b44b7e9aa751508f02c1020e341e0212a9aa146b7428eb5ffea310be27')

prepare() {
  cd "cli-$pkgver"
}

build() {
    cd "cli-$pkgver"
    export GOOS=windows
    export GOROOT=${MINGW_PREFIX}/lib/go
    export CGO_CPPFLAGS="${CPPFLAGS}"
    export CGO_CFLAGS="${CFLAGS}"
    export CGO_CXXFLAGS="${CXXFLAGS}"
    export CGO_LDFLAGS="${LDFLAGS}"
    export GO_LDFLAGS="-s -w"
    export GOFLAGS="-trimpath -mod=readonly -modcacherw -ldflags=-linkmode=external"
    case "${CARCH}" in
      i686|x86_64)
        GOFLAGS+=" -buildmode=pie"
        ;;
    esac

    make GH_VERSION="v$pkgver" bin/gh.exe manpages
    bin/gh.exe completion -s bash | install -Dm644 /dev/stdin bash-completion/completions/gh
    bin/gh.exe completion -s zsh | install -Dm644 /dev/stdin zsh/site-functions/_gh
    bin/gh.exe completion -s fish | install -Dm644 /dev/stdin fish/vendor_completions.d/gh.fish
}

check(){
    cd "cli-$pkgver"
    make test
}

package() {
    cd "cli-$pkgver"
    install -Dm755 "${srcdir}/gh" "${pkgdir}${MINGW_PREFIX}/bin/gh"
    install -Dm755 "bin/gh.exe" "${pkgdir}${MINGW_PREFIX}/bin/gh.exe"
    cp -r share/ "${pkgdir}${MINGW_PREFIX}/"
    install -Dm644 "LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
    install -Dm644 "README.md" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/README.md"
    install -Dm644 bash-completion/completions/gh "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/gh"
    install -Dm644 zsh/site-functions/_gh "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_gh"
    install -Dm644 fish/vendor_completions.d/gh.fish "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/gh.fish"
}
